function bciRun(fileBaseName,run,trainOrTest,option)
% bciRun(fileBaseName,run,trainOrTest,option)
% trainOrTest - 'train' to start training 
%               'test' to start testing 
% option - currently specifies a target Object for SSVEP/P300 
%          paradigms 0...n-1, option = n -> unsupervised feedback
%          option>n -> unsupervised + gripper
% required toolboxes: fieldtrip,spider,chronux
% use_spider;

global GLOBALbci;
global GLOBALtrainDat;
global MEMdata;
global MEMepoch;

if exist('svm','file')~=2,
    error('Check toolbox pathes');
end

% initialisieren:
GLOBALbci=bciGetParam(GLOBALbci);
if ~isfield(GLOBALbci,'srate'),
    [hdr,GLOBALbci]=bciReadRecorderHeader(GLOBALbci); % get header information
end
GLOBALbci=bciInit(GLOBALbci);

% training starten:
if strcmpi(trainOrTest,'train'),
    if GLOBALbci.paradigm.id<10,
        [bci,Dat]=bciMotorStim(GLOBALbci);
    else
        if nargin==4,
            GLOBALbci.paradigm.attendObjectID = option;
        end
        if any(GLOBALbci.paradigm.id==[12,14]),
            [bci,Dat]=bciP3Stim(GLOBALbci);            
        else
            [bci,Dat]=bciSSVEPStim(GLOBALbci);
        end
    end
elseif strcmpi(trainOrTest,'test'),
    if GLOBALbci.paradigm.id<10,
        [bci,Dat]=bciMotorFeedback(GLOBALbci);
    else
        GLOBALbci.paradigm.feedbackType=0; % default is supervised feedback
        if nargin==4,
            if option>=length(GLOBALbci.eventsToClassify),
                warning('Performing unsupervised FeedbackRun');                
                GLOBALbci.param.retrainClassifier=false;
                if option==length(GLOBALbci.eventsToClassify),
                    GLOBALbci.paradigm.feedbackType=1; %unsupervised Feedback
                elseif option==length(GLOBALbci.eventsToClassify)+1,
                    GLOBALbci.paradigm.feedbackType=2; % unsupervised + gripper
                    GLOBALbci.numTrials = 1;
                    fprintf('Number of trials per class set to one.\n');
                elseif option==length(GLOBALbci.eventsToClassify)+2,
                    GLOBALbci.paradigm.feedbackType=3; % gripper + cue
                    GLOBALbci.numTrials = 1;
                    fprintf('Number of trials per class set to one.\n');
                else
                    error('Option parameter not defined.');
                end
                option=0;
            end
            GLOBALbci.paradigm.attendObjectID = option;            
        end
        if any(GLOBALbci.paradigm.id==[11 13]),
            [bci,Dat]=bciSSVEPFeedback(GLOBALbci);
        else
            [bci,Dat]=bciP3Feedback(GLOBALbci);
        end
    end
else
    error('Specify ''train'' or ''test''!');
end

if bci.param.saveRawDat,
    trainDat = MEMdata(:,1:Dat.nRawData);
    save([fileBaseName int2str(run) '_Rawdata'],'trainDat');
    clear trainDat
%     % save raw data as binary file
%     fid = fopen(['rawdata' int2str(run) '.dat'],'wb');
%     fwrite(fid,size(MEMdata,1),'double');
%     fwrite(fid,MEMdata(:,1:Dat.nRawData),'single');
%     fclose(fid);

end
if bci.param.saveEpochs,
    trainEpoch = single(MEMepoch(:,:,1:Dat.nEpochs));
    save([fileBaseName int2str(run) '_Epoch'],'trainEpoch');
    clear trainEpoch
end

% save params and Dat
save([fileBaseName int2str(run)],'bci','Dat');

GLOBALtrainDat{run}=Dat;
GLOBALtrainDat{run}.trainEpoch=MEMepoch(:,:,1:Dat.nEpochs);
GLOBALbci = bci;